plt.clf()
plt.plot(leafrange, pc_query_sklearnKDTree_k5_time/inps.nr_of_repetitions, 'kx-', label='query sklearnKDTree k=5')
plt.plot(leafrange, (pc_query_sklearnKDTree_k5_time)/inps.nr_of_repetitions+pc_generate_sklearnKDTree_time/inps.nr_of_repetitions_generate, 'x-', c='gray', label='create + query sklearnKDTree k=5')
plt.title('sklearnKDTree (single core): generation and query (AMD3900X: 12 cores)')
plt.grid()
plt.xlabel('leafsize')
plt.ylabel('Query time (s)')
plt.xlim([8,30])
plt.xticks(np.arange(8,30,step=2))
#plt.ylim([0,2])
plt.legend()
plt.savefig('figs/pc_sklearnKDTree_AMD3900X_12cores.png', dpi=300, orientation='landscape')

plt.clf()
plt.plot(leafrange, pc_query_cKDTree_k5_time/inps.nr_of_repetitions, 'kx-', label='query cKDTree k=5')
plt.plot(leafrange, (pc_query_cKDTree_k5_time)/inps.nr_of_repetitions+pc_generate_cKDTree_time/inps.nr_of_repetitions_generate, 'x-', c='gray', label='create + query cKDTree k=5')
plt.plot(leafrange, pc_query_cKDTree_k50_time/inps.nr_of_repetitions, 'o-', c='darkblue', label='query cKDTree k=50')
plt.plot(leafrange, (pc_query_cKDTree_k50_time)/inps.nr_of_repetitions+pc_generate_cKDTree_time/inps.nr_of_repetitions_generate, 'o-', c='lightblue', label='create + query cKDTree k=50')
plt.plot(leafrange, pc_query_cKDTree_k500_time/inps.nr_of_repetitions, '+-', c='darkred', label='query cKDTree k=500')
plt.plot(leafrange, (pc_query_cKDTree_k500_time)/inps.nr_of_repetitions+pc_generate_cKDTree_time/inps.nr_of_repetitions_generate, '+-', c='red', label='create + query cKDTree k=500')
plt.title('cKDTree (multi-core): generation and query (%s)'%(inps.cpuname))
plt.grid()
plt.xlabel('leafsize')
plt.ylabel('Query time (s)')
plt.xlim([8,30])
plt.xticks(np.arange(8,30,step=2))
#plt.ylim([0,2])
plt.legend()
plt.savefig('figs/pc_cKDTree_%s.png'%(inps.cpuname), dpi=300, orientation='landscape')

plt.clf()
plt.plot(leafrange, (pc_query_sklearnKDTree_k5_time)/inps.nr_of_repetitions+pc_generate_sklearnKDTree_time/inps.nr_of_repetitions_generate, 'x-', c='k', label='create + query sklearnKDTree k=5 (single core)')
plt.plot(leafrange, (pc_query_cKDTree_k5_time)/inps.nr_of_repetitions+pc_generate_cKDTree_time/inps.nr_of_repetitions_generate, 'o-', c='darkblue', label='create + query cKDTree k=5 (multi core)')
plt.title('sklearnKDTree and cKDTree comparison (%s)'%(inps.cpuname))
plt.grid()
plt.xlabel('leafsize')
plt.ylabel('Query time (s)')
plt.xlim([8,30])
plt.xticks(np.arange(8,30,step=2))
plt.yscale('log')
#plt.ylim([0,2])
plt.legend()
plt.savefig('figs/pc_sklearnKDTree_cKDTree_k5_%s'%(inps.cpuname), dpi=300, orientation='landscape')

fig = plt.figure(figsize=(16.53,11.69), dpi=300)
ax1 = fig.add_subplot(121)
ax1.plot(pc_query_sklearnKDTree_k5_stats[4,:,0], pc_query_cKDTree_k5_stats[4,:,0], '.', c='gray', ms=1, label='mean distance')
ax1.plot([0,pc_query_cKDTree_k5_stats[4,:,0].max()], [0, pc_query_cKDTree_k5_stats[4,:,0].max()], 'k-', label='1:1')
ax1.set_title('Mean distance for each point query (k=5) between sklearnKDTree and cKDTree')
ax1.grid()
ax1.set_xlabel('sklearnKDTree mean distance (k=5)')
ax1.set_ylabel('cKDTree mean distance (k=5)')
ax1.legend()
ax1.set_xlim([0, pc_query_cKDTree_k5_stats[4,:,0].max()])
ax1.set_ylim([0, pc_query_cKDTree_k5_stats[4,:,0].max()])
ax1.set_aspect('equal', 'box')

ax2 = fig.add_subplot(122)
ax2.grid()
ax2.set_title('QQ Plot between sklearnKDTree and cKDTree distances')
#poor man's qqplot
percs = np.linspace(0,100,100)
qn_a = np.percentile(pc_query_sklearnKDTree_k5_stats[4,:,0]-np.median(pc_query_sklearnKDTree_k5_stats[4,:,0]), percs)
qn_b = np.percentile(pc_query_cKDTree_k5_stats[4,:,0]-np.median(pc_query_cKDTree_k5_stats[4,:,0]), percs)
ax2.plot(qn_a,qn_b, 'ko')
#calculate 1:1 line
x = np.linspace(np.min((qn_a.min(),qn_b.min())), np.max((qn_a.max(),qn_b.max())))
ax2.plot(x,x, color="r", ls="--")
ax2.set_xlabel('percentile sklearnKDTree mean distance (k=5)')
ax2.set_ylabel('percentile cKDTree mean distance (k=5)')
ax2.set_aspect('equal', 'box')
fig.tight_layout()
fig.savefig('figs/pc_mean_distance_sklearnKDTree_cKDTree_leafsize16_k5_%s.png'%(inps.cpuname))

print('Max. Difference between mean distance of k=5 neighbors: %3.2f'%(np.max(pc_query_sklearnKDTree_k5_stats[4,:,0]-pc_query_cKDTree_k5_stats[4,:,0])))
print('Median Difference between mean distance of k=5 neighbors: %3.2f'%(np.median(pc_query_sklearnKDTree_k5_stats[4,:,0]-pc_query_cKDTree_k5_stats[4,:,0])))


fig = plt.figure()
ax = plt.subplot(111)
ax.plot(leafrange, (pc_query_pyKDTree_k5_time)/inps.nr_of_repetitions+pc_generate_pyKDTree_time/inps.nr_of_repetitions_generate, 'x-', c='k', label='create + query pyKDTree k=5')
ax.plot(leafrange, (pc_query_cKDTree_k5_time)/inps.nr_of_repetitions+pc_generate_cKDTree_time/inps.nr_of_repetitions_generate, 'o-', c='k', label='create + query cKDTree k=5')
ax.plot(leafrange, (pc_query_pyKDTree_k50_time)/inps.nr_of_repetitions+pc_generate_pyKDTree_time/inps.nr_of_repetitions_generate, 'x-', c='darkblue', label='create + query pyKDTree k=50')
ax.plot(leafrange, (pc_query_cKDTree_k50_time)/inps.nr_of_repetitions+pc_generate_cKDTree_time/inps.nr_of_repetitions_generate, 'o-', c='darkblue', label='create + query cKDTree k=50')
ax.plot(leafrange, (pc_query_pyKDTree_k500_time)/inps.nr_of_repetitions+pc_generate_pyKDTree_time/inps.nr_of_repetitions_generate, 'x-', c='darkred', label='create + query pyKDTree k=500')
ax.plot(leafrange, (pc_query_cKDTree_k500_time)/inps.nr_of_repetitions+pc_generate_cKDTree_time/inps.nr_of_repetitions_generate, 'o-', c='darkred', label='create + query cKDTree k=500')
ax.set_title('pyKDTree and cKDTree comparison (%s)'%(inps.cpuname))
ax.grid()
ax.set_xlabel('leafsize')
ax.set_ylabel('Query time (s)')
ax.set_xlim([8,30])
ax.set_xticks(np.arange(8,30,step=2))
ax.set_yscale('log')
box = ax.get_position()
ax.set_position([box.x0, box.y0 + box.height * 0.2,
                 box.width, box.height * 0.8])
ax.legend(loc='upper center', bbox_to_anchor=(0.5, -0.2),
          fancybox=False, shadow=False, ncol=2,fontsize=8)
fig.savefig('figs/pc_pyKDTree_cKDTree_k5_%s.png'%(inps.cpuname), dpi=300, orientation='landscape')
